package dongze.com.ckdj.persistence;

import android.util.Log;

import java.util.Date;

import dongze.com.ckdj.persistence.dao.DaoSession;
import dongze.com.ckdj.persistence.dao.Image;
import dongze.com.ckdj.persistence.dao.MartinImageCheckInRecord;
import dongze.com.ckdj.persistence.dao.MassLossUploadRecord;
import dongze.com.ckdj.persistence.dao.NamePlateUploadRecord;
import dongze.com.ckdj.persistence.dao.ServerUploadTask;
import dongze.com.ckdj.persistence.dao.State;
import dongze.com.ckdj.service.ImageUploadTask;
import dongze.com.ckdj.service.uploader.UploaderFactory;

/**
 * Created by fanqwz on 2017/8/22.
 */

public class DaoHelper {
    private static final String TAG = DaoHelper.class.getSimpleName();

    public static ImageUploadTask createMassLossTask(DaoSession session,String vin, String user, String massLossRecord, String imageIndex, String imagePath) {
        MassLossUploadRecord record = new MassLossUploadRecord();
        record.setVin(vin);
        record.setTime(new Date());
        record.setImageIndex(imageIndex);
        record.setImagePath(imagePath);
        record.setMassLossRecord(massLossRecord);
        record.setUser(user);
        session.getMassLossUploadRecordDao().insert(record);

        Image image = new Image();
        image.setFilePath(imagePath);
        session.getImageDao().insert(image);

        ServerUploadTask uploadTask = new ServerUploadTask();
        uploadTask.setImage(image);
        uploadTask.setType(UploaderFactory.TYPE_MASS_LOSS);
        uploadTask.setForce(false);
        uploadTask.setState(State.INIT);
        uploadTask.setRecordId(record.getId());
        session.getServerUploadTaskDao().insert(uploadTask);

        record.setTaskId(uploadTask.getId());
        session.getMassLossUploadRecordDao().update(record);

        ImageUploadTask task = new ImageUploadTask();
        task.setId(uploadTask.getId());
        task.setType(uploadTask.getType());
        task.setState(uploadTask.getState());
        task.setRecordId(record.getId());
        return task;
    }

    public static ImageUploadTask createNamePlateTask(DaoSession session, String vin, String user, String imgIndex, String imagePath, Date time) {
        NamePlateUploadRecord record = new NamePlateUploadRecord();
        record.setVin(vin);
        record.setImgIndex(imgIndex);
        record.setImagePath(imagePath);
        record.setUser(user);
        record.setTime(time);
        session.getNamePlateUploadRecordDao().insert(record);

        Image image = new Image();
        image.setFilePath(imagePath);
        session.getImageDao().insert(image);

        ServerUploadTask uploadTask = new ServerUploadTask();
        uploadTask.setImage(image);
        uploadTask.setType(UploaderFactory.TYPE_NAME_PLATE);
        uploadTask.setForce(false);
        uploadTask.setState(State.INIT);
        uploadTask.setRecordId(record.getId());
        session.getServerUploadTaskDao().insert(uploadTask);

        ImageUploadTask task = new ImageUploadTask();
        task.setId(uploadTask.getId());
        task.setType(uploadTask.getType());
        task.setState(uploadTask.getState());
        task.setRecordId(record.getId());
        return task;
    }
    public static ImageUploadTask createOutLineTask(DaoSession session, String vin, String user, String imgIndex, String imagePath, Date time) {
        NamePlateUploadRecord record = new NamePlateUploadRecord();
        record.setVin(vin);
        record.setImgIndex(imgIndex);
        record.setImagePath(imagePath);
        record.setUser(user);
        record.setTime(time);
        session.getNamePlateUploadRecordDao().insert(record);

        Image image = new Image();
        image.setFilePath(imagePath);
        session.getImageDao().insert(image);

        ServerUploadTask uploadTask = new ServerUploadTask();
        uploadTask.setImage(image);
        uploadTask.setType(UploaderFactory.TYPE_OUT_LINE);
        uploadTask.setForce(false);
        uploadTask.setState(State.INIT);
        uploadTask.setRecordId(record.getId());
        session.getServerUploadTaskDao().insert(uploadTask);

        ImageUploadTask task = new ImageUploadTask();
        task.setId(uploadTask.getId());
        task.setType(uploadTask.getType());
        task.setState(uploadTask.getState());
        task.setRecordId(record.getId());
        return task;
    }
    public static ImageUploadTask createRandomInspectionTask(DaoSession session, String vin, String user, String imgIndex, String imagePath, Date time) {
        NamePlateUploadRecord record = new NamePlateUploadRecord();
        record.setVin(vin);
        record.setImgIndex(imgIndex);
        record.setImagePath(imagePath);
        record.setUser(user);
        record.setTime(time);
        session.getNamePlateUploadRecordDao().insert(record);

        Image image = new Image();
        image.setFilePath(imagePath);
        session.getImageDao().insert(image);

        ServerUploadTask uploadTask = new ServerUploadTask();
        uploadTask.setImage(image);
        uploadTask.setType(UploaderFactory.TYPE_RANDOM_INSPECTION);
        uploadTask.setForce(false);
        uploadTask.setState(State.INIT);
        uploadTask.setRecordId(record.getId());
        session.getServerUploadTaskDao().insert(uploadTask);

        ImageUploadTask task = new ImageUploadTask();
        task.setId(uploadTask.getId());
        task.setType(uploadTask.getType());
        task.setState(uploadTask.getState());
        task.setRecordId(record.getId());
        return task;
    }

    public static ImageUploadTask createMartinCheckInTask(DaoSession session,String vin, String user, String typeName, int imageIndex, String imagePath) {
        MartinImageCheckInRecord record = new MartinImageCheckInRecord();
        record.setVin(vin);
        record.setTime(new Date());
        record.setImageIndex(imageIndex);
        record.setImagePath(imagePath);
        record.setTypeName(typeName);
        record.setUser(user);
        session.getMartinImageCheckInRecordDao().insert(record);

        Image image = new Image();
        image.setFilePath(imagePath);
        session.getImageDao().insert(image);

        ServerUploadTask uploadTask = new ServerUploadTask();
        uploadTask.setImage(image);
        uploadTask.setType(UploaderFactory.TYPE_MARTIN_CHECK_IN);
        uploadTask.setForce(false);
        uploadTask.setState(State.INIT);
        uploadTask.setRecordId(record.getId());
        session.getServerUploadTaskDao().insert(uploadTask);

        record.setTaskId(uploadTask.getId());
        session.getMartinImageCheckInRecordDao().update(record);

        ImageUploadTask task = new ImageUploadTask();
        task.setId(uploadTask.getId());
        task.setType(uploadTask.getType());
        task.setState(uploadTask.getState());
        task.setRecordId(record.getId());
        return task;
    }

    public static ImageUploadTask createImageTask(DaoSession session, String imagePath) {
        Image image = new Image();
        image.setFilePath(imagePath);
        session.getImageDao().insert(image);

        ServerUploadTask uploadTask = new ServerUploadTask();
        uploadTask.setImage(image);
        uploadTask.setType(UploaderFactory.TYPE_IMAGE);
        uploadTask.setForce(false);
        uploadTask.setState(State.INIT);
        uploadTask.setRecordId(-1L);
        session.getServerUploadTaskDao().insert(uploadTask);

        ImageUploadTask task = new ImageUploadTask();
        task.setId(uploadTask.getId());
        task.setType(uploadTask.getType());
        task.setState(uploadTask.getState());
        task.setRecordId(-1L);
        return task;
    }
}
